<template>
  <div>
    <!-- <van-nav-bar left-arrow>
      <template #left>
        <van-icon name="arrow-left" @click="$router.back()" />
      </template>
    </van-nav-bar> -->
    <h2>请输入手机号码</h2>
    <p class="title">请输入您常用手机号码，进行登录</p>
    <input
      type="text"
      placeholder="请输入您的手机号"
      class="input"
      v-model="ruleForm2.tel"
    />
    <button @click="submitForm('ruleForm2')">登录</button>

    <p class="fangshi">------其他登录方式------</p>
    <ul>
      <li>
        <a>
          <img
            style="width: 30px; height: 30px; border-radius: 50%"
            src="../../assets/支付.jpg"
            alt=""
          />
          <p>支付宝</p>
        </a>

        <a>
          <img
            style="width: 30px; height: 30px; border-radius: 50%"
            src="../../assets/微信1.jpg"
            alt=""
          />
          <p>微信</p>
        </a>
        <a>
          <img
            style="width: 30px; height: 30px; border-radius: 50%"
            src="../../assets/000.png"
            alt=""
          />
          <p>免验证</p>
        </a>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    // <!--验证手机号是否合法-->
    let checkTel = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入手机号码"));
      } else if (!this.checkMobile(value)) {
        callback(new Error("手机号码不合法"));
      } else {
        callback();
      }
    };
    //  <!--验证码是否为空-->
    let checkSmscode = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入手机验证码"));
      } else {
        callback();
      }
    };
    // <!--验证密码-->
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        }
        callback();
      }
    };
    // <!--二次验证密码-->
    let validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm2.pass) {
        callback(new Error("两次密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      active: 2,
      ruleForm2: {
        pass: "",
        checkPass: "",
        tel: "",
        smscode: "",
      },
      rules2: {
        pass: [{ validator: validatePass, trigger: "change" }],
        checkPass: [{ validator: validatePass2, trigger: "change" }],
        tel: [{ validator: checkTel, trigger: "change" }],
        smscode: [{ validator: checkSmscode, trigger: "change" }],
      },
      buttonText: "发送验证码",
      isDisabled: false, // 是否禁止点击发送验证码按钮
      flag: true,
    };
  },
  methods: {
    // <!--发送验证码-->
    sendCode() {
      let tel = this.ruleForm2.tel;
      if (this.checkMobile(tel)) {
        console.log(tel);
        let time = 60;
        this.buttonText = "已发送";
        this.isDisabled = true;
        if (this.flag) {
          this.flag = false;
          let timer = setInterval(() => {
            time--;
            this.buttonText = time + " 秒";
            if (time === 0) {
              clearInterval(timer);
              this.buttonText = "重新获取";
              this.isDisabled = false;
              this.flag = true;
            }
          }, 1000);
        }
      }
    },
    // <!--提交注册-->
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          setTimeout(() => {
            this.$router.push({
              path: "/shome",
            });
          }, 400);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // <!--进入登录页-->
    // 验证手机号
    checkMobile(str) {
      let re = /^1\d{10}$/;
      if (re.test(str)) {
        return true;
      } else {
        return false;
      }
    },
  },
};
</script>

<style  lang="scss" scoped>
* {
  box-sizing: border-box;
}
h2 {
  display: block;
  color: #111;
  padding: 30px 0 20px 20px;
}
.title {
  font-size: 14px;
  font-size: 100;
  padding: 20px 0 20px 20px;
  color: gray;
}
.input {
  width: 90%;
  height: 40px;
  border: 1px solid #999;
  padding-left: 20px;
  margin-left: 20px;
  border-radius: 5px;
}
.fangshi {
  display: block;
  text-align: center;
  padding: 90px 0 40px 0;
}
button {
  width: 90%;
  height: 40px;
  background: #409eff;
  color: white;
  border: 0;
  margin: 30px 0 10px 20px;
  border-radius: 5px;
}
ul {
  width: 100%;
  height: 60px;

  li {
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    img {
      display: block;
      margin: 0 auto;
    }
    p {
      padding: 10px 0;
    }
  }
}
</style>